package com.frinder.official.akka.actors;

import com.frinder.official.spring.MyBaseActor;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * Created by frinder on 2017/9/13.
 * <p>
 * 注：测试失败！
 */
@Component
@Scope("prototype")
public class HotSwapActor extends MyBaseActor {

    private Receive angry;
    private Receive happy;

    public HotSwapActor() {
        angry = receiveBuilder()
                .matchEquals("foo", s -> {
                    log.debug("{}", s);
                    getSender().tell("I am already angry?", getSelf());
                })
                .matchEquals("bar", s -> {
                    log.debug("{}", s);
                    getContext().become(happy);
                })
                .build();

        happy = receiveBuilder()
                .matchEquals("bar", s -> {
                    log.debug("{}", s);
                    getSender().tell("I am already happy :-)", getSelf());
                })
                .matchEquals("foo", s -> {
                    log.debug("{}", s);
                    getContext().become(angry);
                })
                .build();
    }

    @Override
    public Receive createReceive() {
        return receiveBuilder()
                .matchEquals("foo", s -> {
                    log.debug("{}", s);
                    getContext().become(angry);
                })
                .matchEquals("bar", s ->
                        getContext().become(happy)
                )
                .build();
    }

}
